package org.opens.lambda.date;

import cn.hutool.core.date.DateUtil;
import org.junit.Test;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.util.Date;

public class LocalDateAndLocalTime {

    /**
     * 输出:
     *      getYear方法输出为: 2016
     *      getMonth方法输出为: NOVEMBER
     *      getMonthValue方法输出为: 11
     *      getDayOfYear方法输出为: 318
     *      getDayOfMonth方法输出为: 13
     *      getDayOfWeek方法输出为: SUNDAY
     * 说明:
     *      1. getMonth获取的是英文月, 而getMonthValue获取的是数字月;
     *          - 这个月从1开始计数.
     *      2. This class is immutable and thread-safe.
     *          - 中文: 这个类是不可变的, 线程安全的.
     */
    @Test
    public void test1() {
        LocalDate date = LocalDate.of(2016, 11, 13);
        System.out.println("getYear方法输出为: " + date.getYear());
        System.out.println("getMonth方法输出为: " + date.getMonth());
        System.out.println("getMonthValue方法输出为: " + date.getMonthValue());
        System.out.println("getDayOfYear方法输出为: " + date.getDayOfYear());
        System.out.println("getDayOfMonth方法输出为: " + date.getDayOfMonth());
        System.out.println("getDayOfWeek方法输出为: " + date.getDayOfWeek());
        System.out.println("getDayOfWeek方法输出为: " + date.getDayOfWeek());
    }

    /**
     * 输出:
     *      2019-12-15
     *      349
     * 说明:
     *      1. 不需要格式化, toString方法直接输出格式化之后的格式.
     */
    @Test
    public void test2() {
        LocalDate now = LocalDate.now();

        System.out.println(now);

        //下面这行会报错, 因为在java8的api中, date就是date不包含时间, time就是time不包含date.
        //System.out.println(now.get(ChronoField.HOUR_OF_AMPM));

        //获取关于日期的就没事
        System.out.println(now.get(ChronoField.DAY_OF_YEAR));
    }

    /**
     * 输出:
     *      4
     *      16:47:51.351
     * 说明:
     *      1. 可以发现, LocalTime不需要格式化;
     *          - 如果可以应用到spring MVC和mybatis中那就非常不错了.
     */
    @Test
    public void test3() {
        LocalTime now = LocalTime.now();

        //可以发现, 在LocalTime上调用这个方法就不会报错, 但是在LocalDate上就会报错
        System.out.println(now.get(ChronoField.HOUR_OF_AMPM));

        //同样的, LocalTime上不能获取date相关的, 会报错: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: DayOfYear
        //System.out.println(now.get(ChronoField.DAY_OF_YEAR));

        System.out.println(now);
    }

    @Test
    public void test4() {
        LocalTime time = LocalTime.of(12, 23, 45);

        System.out.println("直接输出-> " + time);
        System.out.println("getHour输出-> " + time.getHour());
        System.out.println("getMinute输出-> " + time.getMinute());
        System.out.println("getSecond输出-> " + time.getSecond());
        System.out.println("getNano输出-> " + time.getNano());
    }

    @Test
    public void conformLocalDateAndLocalTime() {
        LocalDate nowDate = LocalDate.now();
        LocalTime nowTime = LocalTime.now();

        LocalDateTime dateTime = LocalDateTime.of(nowDate, nowTime);
        System.out.println(dateTime);
    }

    /**
     * 功能:
     *      将LocalDateTime的年龄设置为指定的值.
     * 输出:
     *      2019-12-27T18:00:09.065
     *      2020-12-27T18:00:09.065
     * 注意:
     *      1. withYear方法时整体设置, 所以需要在原基础上进行偏移.
     */
    @Test
    public void test31() {
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDateTime dateTime1 = dateTime.withYear(LocalDateTime.now().getYear() + 1);
        System.out.println(dateTime);
        System.out.println(dateTime1);
    }

    @Test
    public void test32() {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate localDate = LocalDate.parse("20120512", pattern);
        System.out.println(localDate.atStartOfDay());
    }

    @Test
    public void test33() {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime localDate = LocalDateTime.parse("20120512000000", pattern);
        System.out.println(localDate);
    }

    @Test
    public void test34() {
        System.out.println(DateUtil.format(new Date(1623081600000L), "yyyy-MM-dd hh:mm:ss"));
    }

    @Test
    public void test35() {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyyMMdd");
        String d1 = "20120506";
        Date date = Date.from(LocalDate.parse(d1, pattern).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(date);
    }

    @Test
    public void test36() {
        Date date = new Date();
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDateTime dateTime = LocalDateTime.of(localDate, LocalTime.of(23, 59, 59));
        System.out.println(dateTime);
    }

}
